package org.example.ThreadDemo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    //构建可重入锁对象
    static Lock  lock  = new ReentrantLock();

    public static void method1(){
        try{
            lock.lock();                        //获取锁
            //进入method2方法
            method2();
            //可能会出现线程安全的操作
            System.out.println("method1...");
        }finally{
            //尽量在finally中释放锁
            lock.unlock();                      //释放锁
        }
    }

    public static void method2(){
        try{
            lock.lock();
            //可能会出现线程安全的操作
            System.out.println("method2...");
        }finally{
            //尽量在finally中释放锁
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        method1();
    }
}